<?php

class BuyerController extends Controller {

	protected $db;
	
	function __construct($db) {
	
		$this->db = $db;
		
		if (isset($_GET['view'])) {
			$viewItem = $_GET['view'];
			if ($viewItem == 'movie') {
				if (isset($_POST['action'])) {
					if ($_POST['action'] == 'save') {
						if (isset($_SESSION['orderLines'])) {
							$temp = array();
							$temp[0] = $_POST['movieId'];
							$temp[1] = $_POST['title'];
							$temp[2] = $_POST['price'] * $_POST['copies'];
							$temp[3] = $_POST['copies'];
							$orderLine = implode('_', $temp);
							$_SESSION['orderLines'] = $_SESSION['orderLines'].'[]'.$orderLine;
						}
						else {
							$i = 0;
							$temp = array();
							$temp[0] = $_POST['movieId'];
							$temp[1] = $_POST['title'];
							$temp[2] = $_POST['price'] * $_POST['copies'];
							$temp[3] = $_POST['copies'];
							$orderLine = implode('_', $temp);
							$_SESSION['orderLines'] = $orderLine;
						}
						$this->view = new ConfirmationMessageView('<p>Order added to the shopping cart</p>');
						$this->view->setTitle('Order');
						return;
					}
				} 
				$id = 1;
				if (isset($_GET['movieId'])) {
					$id = $_GET['movieId'];
				}
				$movie = $this->getMovieById($id);
				$movieList = $this->getSuggestions($id);
				$actorsPlayed = $this->getMovieActors($id);
				$this->view = new MovieView();
				$this->view->setMovie($movie);
				$this->view->setMovieList($movieList);
				$this->view->setActorsPlayed($actorsPlayed);
				$this->view->setTitle("Movie");
			}
			else if ($viewItem == 'checkout') {
				if (isset($_POST['action'])) {
					if ($_POST['action'] == 'save') {
						$ordersRaw = $_POST['orders'];
						$shipping = $_POST['shipping'];
						$creditType = $_POST['creditType'];
						$creditNumber = $_POST['creditNumber'];
						$memberId = $_POST['memberId'];
						$orders = explode('[]', $ordersRaw);
						$orderId = $this->generateRandomString(20);
						for ($i=0 ; $i<sizeof($orders) ; $i++) {
							$orderTokens = explode('_', $orders[$i]);
							$movieId = $orderTokens[0];
							$price = $orderTokens[2];
							$copies = $orderTokens[3];
							$currentOrder = new Order('', $orderId, $movieId, $memberId, $shipping, $price, $copies, date("Y-m-d"));
							$currentOrder->setCreditType($creditType);
							$currentOrder->setCreditNumber($creditNumber);
							$allOrders[$i] = $currentOrder;
						}
						
						if ($this->storeOrders($allOrders)) {
							$this->view = new ConfirmationMessageView('<p>Transaction was committed successfully</p>');
							$this->view->setTitle('Success');
							unset($_SESSION['orderLines']);
						}
						else {
							$this->view = new ConfirmationMessageView('<p>One or more movies had not enough of the requested copies</p>');
							$this->view->setTitle('Failure');
						}
					}
				} else {
					$user = $this->getMember($_SESSION['username']);
					$theOrders = $_SESSION['orderLines'];
					$this->view = new CheckoutView();
					$this->view->setTitle("Checkout");
					$this->view->setOrders($theOrders);
					$this->view->setUserInfo($user);
				}
			}
		}
	}
	
	function getMovieById($id) {
		$results = $this->db->getMovie($id);
		$i=0;
		$row = $results->fetch_row();
		$movie = new Movie($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]);
		return $movie;
	}
	
	function getMember($u) {
		$results = $this->db->getMember($u);
		$row = $results->fetch_row();
		$user = new Member($row[0], $row[1], $row[2], $row[4], $row[5], $row[6], $row[7], $row[8]);
		return $user;
	}
	
	function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';
		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, strlen($characters) - 1)];
		}
		return $randomString;
	}
	
	function storeOrders($orders) {
		
		$serOrders = array();
		$i = 0;
		foreach ($orders as $order) {
			$orderId = $order->getOrderId();
			$memberId = $order->getIdMember();
			$mId = $order->getMovieTitle();
			$shipping = $order->getShipping();
			$price = $order->getPrice();
			$copies = $order->getNoCopies();
			$date = $order->getOdate();
			$creditType = $order->getCreditType();
			$creditNumber = $order->getCreditNumber();
			$serOrder = $orderId.'_'.$memberId.'_'.$mId.'_'.$shipping.'_'.$price.'_'.$copies.'_'.$date.'_'.$creditType.'_'.$creditNumber;
			$serOrders[$i] = $serOrder;
			$i++;
		}
		$serString = implode('[]', $serOrders);
		
		return $this->db->storeOrder($serString);
	
	}
	
	function getSuggestions($m) {
		$results = $this->db->getSuggestedMovies($m);
		$mList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$movie = new Movie($row[0], $row[1], $row[2], null, $row[3], $row[4], $row[5], $row[6], $row[7], null);
			$mList[$i] = $movie;
			$i++;
		}
		return $mList;
	}
	
	function getMovieActors($mId) {
		$results = $this->db->getMovieActors($mId);
		$aList = array();
		$i=0;
		while ($row = $results->fetch_row()) {
			$actor = new Actor($row[0], $row[1], $row[2], $row[3]);
			$aList[$i] = $actor;
			$i++;
		}
		return $aList;
	}

}

?>